'    Copyright 2008 Jan Molnar <jan.molnar@abds.de>
'
'    This file is part of JCQ.

'    JCQ is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 2 of the License, or
'    (at your option) any later version.

'    JCQ is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.

'    You should have received a copy of the GNU General Public License
'    along with JCQ. If not, see <http://www.gnu.org/licenses/>.

Imports System.ComponentModel
Imports System.Xml

'Public Class DefaultValueFormatter
'    Implements IValueFormatter

'    Private _Context As ISerializer
'    Private _ValueType As Type
'    Private _Converter As TypeConverter

'    Public Sub New(ByVal context As ISerializer, ByVal type As Type)
'        _Context = context
'        _ValueType = type
'        _Converter = TypeDescriptor.GetConverter(type)
'    End Sub

'    Public ReadOnly Property ValueType() As Type
'        Get
'            Return _ValueType
'        End Get
'    End Property

'    Public Function Serialize(ByVal graph As Object) As String Implements IValueFormatter.Serialize
'        Return _Converter.ConvertToString(graph)
'    End Function

'    Public Function Deserialize(ByVal type As System.Type, ByVal value As String) As Object Implements IValueFormatter.Deserialize
'        Return _Converter.ConvertFromString(value)
'    End Function
'End Class

Public Class XmlValueFormatter
    Implements IValueFormatter

    Private ReadOnly _Context As ISerializer
    Private ReadOnly _ValueType As Type
    Private ReadOnly _ValueTypeCode As TypeCode

    Public Sub New(ByVal context As ISerializer, ByVal type As Type)
        _Context = context
        _ValueType = type
        _ValueTypeCode = System.Type.GetTypeCode(_ValueType)
    End Sub

    Public Function Serialize(ByVal graph As Object) As String Implements IValueFormatter.Serialize
        Select Case _ValueTypeCode
            Case TypeCode.Boolean
                Return XmlConvert.ToString(DirectCast(graph, Boolean))
            Case TypeCode.Byte
                Return XmlConvert.ToString(DirectCast(graph, Byte))
            Case TypeCode.Char
                Return XmlConvert.ToString(DirectCast(graph, Char))
            Case TypeCode.DateTime
                Return XmlConvert.ToString(DirectCast(graph, DateTime), XmlDateTimeSerializationMode.Utc)
            Case TypeCode.Decimal
                Return XmlConvert.ToString(DirectCast(graph, Decimal))
            Case TypeCode.Double
                Return XmlConvert.ToString(DirectCast(graph, Double))
            Case TypeCode.Int16
                Return XmlConvert.ToString(DirectCast(graph, Int16))
            Case TypeCode.Int32
                Return XmlConvert.ToString(DirectCast(graph, Int32))
            Case TypeCode.Int64
                Return XmlConvert.ToString(DirectCast(graph, Int64))
            Case TypeCode.SByte
                Return XmlConvert.ToString(DirectCast(graph, SByte))
            Case TypeCode.Single
                Return XmlConvert.ToString(DirectCast(graph, Single))
            Case TypeCode.UInt16
                Return XmlConvert.ToString(DirectCast(graph, UInt16))
            Case TypeCode.UInt32
                Return XmlConvert.ToString(DirectCast(graph, UInt32))
            Case TypeCode.UInt64
                Return XmlConvert.ToString(DirectCast(graph, UInt64))
            Case Else
                Throw New ArgumentException("Type of this TypeCode cannot be serialized.")
        End Select
    End Function

    Public Function Deserialize(ByVal value As String) As Object Implements IValueFormatter.Deserialize
        Select Case _ValueTypeCode
            Case TypeCode.Boolean
                Return XmlConvert.ToBoolean(value)
            Case TypeCode.Byte
                Return XmlConvert.ToByte(value)
            Case TypeCode.Char
                Return XmlConvert.ToChar(value)
            Case TypeCode.DateTime
                Return XmlConvert.ToDateTime(value, XmlDateTimeSerializationMode.Utc)
            Case TypeCode.Decimal
                Return XmlConvert.ToDecimal(value)
            Case TypeCode.Double
                Return XmlConvert.ToDouble(value)
            Case TypeCode.Int16
                Return XmlConvert.ToInt16(value)
            Case TypeCode.Int32
                Return XmlConvert.ToInt32(value)
            Case TypeCode.Int64
                Return XmlConvert.ToInt64(value)
            Case TypeCode.SByte
                Return XmlConvert.ToSByte(value)
            Case TypeCode.Single
                Return XmlConvert.ToSingle(value)
            Case TypeCode.UInt16
                Return XmlConvert.ToUInt16(value)
            Case TypeCode.UInt32
                Return XmlConvert.ToUInt32(value)
            Case TypeCode.UInt64
                Return XmlConvert.ToUInt64(value)
            Case Else
                Throw New ArgumentException("Type of this TypeCode cannot be serialized.")
        End Select
    End Function

    Public ReadOnly Property ValueType() As System.Type Implements IValueFormatter.ValueType
        Get
            Return _ValueType
        End Get
    End Property
End Class